package com.yequan.hbb.adapter;

import android.content.Context;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.yequan.hbb.R;
import com.yequan.hbb.entries.TuanTuanEntries;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Created by DingLong on 2016/10/23.
 */

public class TuanAdapter<D> extends RecyclerView.Adapter<TuanAdapter.ViewHolder> {
    private List<D> list;
    private Context context;
    private final LayoutInflater inflater;

    private Handler handler =new Handler();

    private List<Long> timeList = new ArrayList<>();
    private int position;

    public TuanAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        inflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = null;
        View view = null;
        switch (viewType) {
            case 0:
                view = inflater.inflate(R.layout.item_helpchose, parent, false);
                break;
        }
        viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        this.position = position;
        switch (getItemViewType(position)) {
            case 0:
                TuanTuanEntries.DataBean.DataBean2 bean2 = (TuanTuanEntries.DataBean.DataBean2) list.get(position);
                String coverTitle = bean2.getCoverTitle();
                holder.chose_covertitle.setText(coverTitle);
                holder.chose_img.setImageURI(Uri.parse(bean2.getCoverImage()));
                String endTime = bean2.getEndTime();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CANADA);

                try {
                    Date parse = dateFormat.parse(endTime);
                    long lasttime = parse.getTime();
                    long currentTime = System.currentTimeMillis();
                    long time = (lasttime - currentTime);
                   // timeList.add(position, time);
                   holder.tvtime1.setText(time/1000+"");
                    new CountDownTimer(time, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            holder.tvtime3.setText(millisUntilFinished / 1000 + "s");
                        }

                        @Override
                        public void onFinish() {
                            holder.tvtime3.setText("done");
                        }
                    }.start();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

//    public void start() {
//        handler.postDelayed(runnable, 1000);
//    }
//    Runnable runnable = new Runnable() {
//        @Override
//        public void run() {
//
//            long time = timeList.get(position) - 1;
//            timeList.add(position,time);
//            String formatLongToTimeStr = formatLongToTimeStr(time);
//            String[] split = formatLongToTimeStr.split("：");
//            for (int i = 0; i < split.length; i++) {
//                if (i == 0) {
//                    tvtime1.setText(split[0] + "小时");
//                }
//                if (i == 1) {
//                    tvtime2.setText(split[1] + "分钟");
//                }
//                if (i == 2) {
//                    tvtime3.setText(split[2] + "秒");
//                }
//
//            }
//            if (time > 0) {
//                handler.postDelayed(this, 1000);
//            }
//        }
//    };
    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        Object item = list.get(position);
        if (item instanceof TuanTuanEntries.DataBean.DataBean2) {
            return 0;
        }
        return super.getItemViewType(position);
    }

    public void addAll(Collection<? extends D> collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }

    //格式化时间
    public String formatLongToTimeStr(Long l) {
        int hour = 0;
        int minute = 0;
        int second = 0;
        second = l.intValue();
        if (second > 60) {
            minute = second / 60;         //取整
            second = second % 60;         //取余
        }

        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        String strtime = hour + "：" + minute + "：" + second;
        return strtime;

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvtime1;
        private TextView timeView;
        private TextView chose_covertitle;
        private ImageView chose_img;
        private TextView tvtime2;
        private TextView tvtime3;

        public ViewHolder(View itemView) {
            super(itemView);
            chose_covertitle = (TextView) itemView.findViewById(R.id.chose_covertitle);
            chose_img = (ImageView) itemView.findViewById(R.id.chose_img);
            timeView = ((TextView) itemView.findViewById(R.id.chose_time));
            tvtime1 = ((TextView) itemView.findViewById(R.id.tvtime1));
            tvtime2 = ((TextView) itemView.findViewById(R.id.tvtime2));
            tvtime3 = ((TextView) itemView.findViewById(R.id.tvtime3));
        }
    }

}
